package cn.edu.scau.cmi.ema.service;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Event9slr;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.*;
@Service("Event9slrService")
@Transactional

public class Event9slrServiceImpl extends CmiEmaService implements Event9slrService {
	public Event9slrServiceImpl() {
	}

	@Transactional
	public Event9slr saveEvent9slr(Event9slr event9slr) {

		if (event9slr.getId() == null) {

			Event9slr existingEvent9slr = event9slrDAO.findEvent9slrByPhysicalPrimaryKey(event9slr.getNumber());

			if (isEquals(event9slr, existingEvent9slr)) {
				return existingEvent9slr;
			}

			event9slr = event9slrDAO.store(event9slr);
			event9slrDAO.flush();

		} else {
			Event9slr existingEvent9slr = event9slrDAO.findEvent9slrByPrimaryKey(event9slr.getId());

			event9slr = event9slrDAO.store(event9slr);
			event9slrDAO.flush();
		}

		return event9slr;
	}

	public void deleteEvent9slr(Event9slr event9slr) {
		event9slrDAO.remove(event9slr);
		event9slrDAO.flush();
	}

	@Transactional
	public void deleteEvent9slrs(Set<Integer> deleteEvent9slrsId) {
		Query query = event9slrDAO.createNamedQuery("deleteEvent9slrsById", -1, -1);
		query.setParameter(1, deleteEvent9slrsId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Event9slr> loadEvent9slrs() {
		return event9slrDAO.findAllEvent9slrs();

	}

	@Transactional
	public Set<Event9slr> loadReferenceEvent9slrs(Set<Event9slr> event9slrs) {
		Iterator<Event9slr> event9slrsIterator = event9slrs.iterator();
		Set<Event9slr> referencedEvent9slrs = new HashSet<>();
		while (event9slrsIterator.hasNext()) {
			Event9slr currentEvent9slr = event9slrsIterator.next();

		}
		return referencedEvent9slrs;
	}

	public Set<Event9slr> loadEvent9slrsByIdSet(Set<Integer> event9slrIdSet) {

		Iterator<Integer> event9slrIdIterator = event9slrIdSet.iterator();
		Set<Event9slr> event9slrSet = new HashSet<>();
		while (event9slrIdIterator.hasNext()) {
			Integer id = event9slrIdIterator.next();
			Event9slr currentEvent9slr = event9slrDAO.findEvent9slrById(id);
			event9slrSet.add(currentEvent9slr);
		}
		return event9slrSet;
	}

	@Transactional
	public List<Event9slr> findAllEvent9slrs(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Event9slr>(event9slrDAO.findAllEvent9slrs(startResult, maxRows));
	}

	@Transactional
	public Event9slr findEvent9slrByPrimaryKey(Integer id) {
		return event9slrDAO.findEvent9slrByPrimaryKey(id);
	}

	@Transactional
	public Integer countEvent9slrs() {
		return ((Long) event9slrDAO.createQuerySingleResult("select count(o) from Event9slr o").getSingleResult())
				.intValue();
	}

	@Transactional
	public Integer countEvent9slrs(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Event9slr.class);
		return ((Long) event9slrDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 *
	 */
	@Transactional
	public Integer countEvent9slrsByName(String queryName) {
		Query query = event9slrDAO.createNamedQuery("countEvent9slrsByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countEvent9slrsByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Event9slr.class);
		Session session = (Session) entityManager.getDelegate();
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadEvent9slrObjectReferencedSet(Event9slr event9slr) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadEvent9slrClassReferencedSet(Event9slr event9slr) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();

		return classReferencedSetMap;
	}

	public boolean isEquals(Event9slr event9slr, Object obj) {
		if (obj == event9slr)
			return true;
		if (!(obj instanceof Event9slr))
			return false;
		Event9slr comparedEvent9slr = (Event9slr) obj;
		if (!CheckPropertyEqual(event9slr.getNumber(), comparedEvent9slr.getNumber())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

